home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_100
/
170_01
/
cpcn.c
< prev
next >
Wrap
Text File
|
1980-01-01
|
49KB
|
2,503 lines
/* ### cpcn-1 */
/************************************************/
/* */
/* small-c:PC compiler */
/* */
/* by Ron Cain */
/* modified by CAPROCK SYSTEMS for IBM PC */
/* */
/************************************************/
#define BANNER "* * * Small-C:PC V1.1 * * *"
#define VERSION "PC-DOS Version N: June, 1982"
#define AUTHOR "By Ron Cain, Modified by CAPROCK SYSTEMS for IBM PC"
/* Define system dependent parameters */
/* Stand-alone definitions */
#define NULL 0
#define eol 13
/* UNIX definitions (if not stand-alone) */
/* #include <stdio.h> */
/* #define eol 10 */
/* Define the symbol table parameters */
#define symsiz 14
#define symtbsz 5040
#define numglbs 300
#define startglb symtab
#define endglb startglb+numglbs*symsiz
#define startloc endglb+symsiz
#define endloc symtab+symtbsz-symsiz
/* Define symbol table entry format */
#define name 0
#define ident 9
#define type 10
#define storage 11
#define offset 12
/* System wide name size (for symbols) */
#define namesize 9
#define namemax 8
/* Define data for external symbols */
#define extblsz 2000
#define startextrn exttab
#define endextrn exttab+extblsz-namesize-1
/* Possible types of exttab entries */
/* Stored in the byte following zero terminating the name */
#define rtsfunc 1
#define userfunc 2
#define statref 3
/* Define possible entries for "ident" */
#define variable 1
#define array 2
#define pointer 3
#define function 4
/* Define possible entries for "type" */
#define cchar 1
#define cint 2
/* Define possible entries for "storage" */
#define statik 1
#define stkloc 2
/* Define the "while" statement queue */
#define wqtabsz 100
#define wqsiz 4
#define wqmax wq+wqtabsz-wqsiz
/* Define entry offsets in while queue */
#define wqsym 0
#define wqsp 1
#define wqloop 2
#define wqlab 3
/* Define the literal pool */
#define litabsz 3000
#define litmax litabsz-1
/* Define the input line */
#define linesize 80
#define linemax linesize-1
#define mpmax linemax
/* Define the macro (define) pool */
#define macqsize 1000
#define macmax macqsize-1
/* Define statement types (tokens) */
#define stif 1
#define stwhile 2
#define streturn 3
#define stbreak 4
#define stcont 5
#define stasm 6
#define stexp 7
/* Define how to carve up a name too long for the assembler */
#define asmpref 7
#define asmsuff 7
/* Now reserve some storage words */
char exttab[extblsz]; /* external symbols */
char *extptr; /* pointer to next available entry */
char symtab[symtbsz]; /* symbol table */
char *glbptr,*locptr; /* ptrs to next entries */
int wq[wqtabsz]; /* while queue */
int *wqptr; /* ptr to next entry */
char litq[litabsz]; /* literal pool */
int litptr; /* ptr to next entry */
char macq[macqsize]; /* macro string buffer */
int macptr; /* and its index */
char line[linesize]; /* parsing buffer */
char mline[linesize]; /* temp macro buffer */
int lptr,mptr; /* ptrs into each */
/* Misc storage */
int nxtlab, /* next avail label # */
litlab, /* label # assigned to literal pool */
cextern, /* collecting external names flag */
Zsp, /* compiler relative stk ptr */
argstk, /* function arg sp */
ncmp, /* # open compound statements */
errcnt, /* # errors in compilation */
errstop, /* stop on error gtf 7/17/80 */
eof, /* set non-zero on final input eof */
input, /* iob # for input file */
output, /* iob # for output file (if any) */
input2, /* iob # for "include" file */
ctext, /* non-zero to intermix c-source */
cmode, /* non-zero while parsing c-code */
/* zero when passing assembly code */
lastst, /* last executed statement type */
saveout, /* holds output ptr when diverted to console */
/* gtf 7/16/80 */
fnstart, /* line# of start of current fn. gtf 7/2/80 */
lineno, /* line# in current file gtf 7/2/80 */
infunc, /* "inside function" flag gtf 7/2/80 */
savestart, /* copy of fnstart " " gtf 7/16/80 */
saveline, /* copy of lineno " " gtf 7/16/80 */
saveinfn; /* copy of infunc " " gtf 7/16/80 */
char *currfn, /* ptr to symtab entry for current fn. gtf 7/17/80 */
*savecurr; /* copy of currfn for #include gtf 7/17/80 */
char quote[2]; /* literal string for '"' */
char *cptr; /* work ptr to any char buffer */
int *iptr; /* work ptr to any int buffer */
/* >>>>> start cc1 <<<<<< */
/* */
/* Compiler begins execution here */
/* */
main()
{
hello(); /* greet user */
see(); /* determine options */
litlab=1;
openin(); /* first file to process */
while (input!=0) /* process user files till he quits */
{
extptr=startextrn; /* clear external symbols */
glbptr=startglb; /* clear global symbols */
locptr=startloc; /* clear local symbols */
wqptr=wq; /* clear while queue */
macptr= /* clear the macro pool */
litptr= /* clear literal pool */
Zsp = /* stack ptr (relative) */
errcnt= /* no errors */
eof= /* not end-of-file yet */
input2= /* no include file */
saveout= /* no diverted output */
ncmp= /* no open compound states */
lastst= /* no last statement yet */
cextern= /* no externs yet */
fnstart= /* current "function" started at line 0 gtf 7/2/80 */
lineno= /* no lines read from file gtf 7/2/80 */
infunc= /* not in function now gtf 7/2/80 */
quote[1]=
0; /* ...all set to zero.... */
quote[0]='"'; /* fake a quote literal */
currfn=NULL; /* no function yet gtf 7/2/80 */
cmode=nxtlab=1; /* enable preprocessing and reset label numbers */
openout();
header();
parse();
if (ncmp) error("missing closing bracket");
extdump();
dumpublics();
trailer();
closeout();
errorsummary();
openin();
}
}
/* ### cpcn-2 */
/* */
/* Abort compilation */
/* gtf 7/17/80 */
abort()
{
if(input2)
endinclude();
if(input)
fclose(input);
closeout();
toconsole();
pl("Compilation aborted."); nl();
exit();
/* end abort */}
/* */
/* Process all input text */
/* */
/* At this level, only static declarations, */
/* defines, includes, and function */
/* definitions are legal... */
parse()
{
while (eof==0) /* do until no more input */
{
if(amatch("extern",6)) {
cextern=1;
if(amatch("char",4)) {declglb(cchar);ns();}
else if(amatch("int",3)) {declglb(cint);ns();}
else {declglb(cint);ns();}
cextern=0;
}
else if(amatch("char",4)){declglb(cchar);ns();}
else if(amatch("int",3)){declglb(cint);ns();}
else if(match("#asm"))doasm();
else if(match("#include"))doinclude();
else if(match("#define"))addmac();
else newfunc();
blanks(); /* force eof if pending */
}
}
/* ### cpcn-3 */
dumpublics()
{
outstr("DUMMY SEGMENT BYTE STACK 'dummy'");nl();
outstr("DUMMY ENDS");nl();
outstr("STACK SEGMENT BYTE PUBLIC 'stack'");nl();
dumplits();
dumpglbs();
outstr("STACK ENDS");nl();
}
extdump()
{
char *ptrext;
ptrext=startextrn;
while(ptrext<extptr)
{
if((cptr=findglb(ptrext))!=0)
{if(cptr[ident]==function)
if(cptr[offset]!=function) outextrn(ptrext);
}
else outextrn(ptrext);
ptrext=ptrext+strlen(ptrext)+2;
}
outstr("CSEG ENDS");nl();
}
/* ### cpcn-4 */
outextrn(ptr)
char *ptr;
{
char *functype;
functype=ptr+strlen(ptr)+1;
if(*functype==statref) return;
ot("EXTRN ");
if(*functype==rtsfunc) outasm(ptr);
else outname(ptr);
col();outstr("NEAR");nl();
}
/* */
/* Dump the literal pool */
/* */
dumplits()
{int j,k;
if (litptr==0) return; /* if nothing there, exit...*/
printlabel(litlab); /* print literal label */
k=0; /* init an index... */
while (k<litptr) /* to loop with */
{defbyte(); /* pseudo-op to define byte */
j=10; /* max bytes per line */
while(j--)
{outdec((litq[k++]&127));
if ((j==0) | (k>=litptr))
{nl(); /* need <cr> */
break;
}
outbyte(','); /* s